package com.xizimall.portal.controller;

import java.io.UnsupportedEncodingException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
/**
 * 搜索controller
 * <p>Title: SearchController</p>
 * <p>Description: </p>
 * <p>华南理工大学研究生</p> 
 * @author	李隆熹
 * @date	2018年4月25日下午12:00:27
 * @version 1.0
 */
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xizimall.portal.pojo.SearchResult;
import com.xizimall.portal.service.SearchService;
@Controller
public class SearchController {
     @Autowired
     private SearchService searchService;
     @RequestMapping("/search")
     public String search(@RequestParam("q")String queryString,@RequestParam
    		 (defaultValue="1")Integer page,Model model){
    	 if(queryString!=null){
    		 try {
				queryString=new String(queryString.getBytes("iso8859-1"),"utf-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
    	 }
    	 SearchResult searchResult=searchService.search(queryString, page);
    	 //向页面传递参数
    	 model.addAttribute("query", queryString);
    	 model.addAttribute("totalPages", searchResult.getPageCount());
    	 model.addAttribute("itemList",searchResult.getItemList());
    	 model.addAttribute("page",page);
    	 return "search";
     }
}
